<?php

class QuestionController extends FController {

    public $user_school_list;
    public $user_subject_list;
    public $user_role_list;
    public $exam_id;

    //权限
    public function accessRules() {
        $config = array(
            'index' => 1,
            'questionadd' => 1,
        );
        return ZRole::auth("role", $config);
    }

    public function __common() {
        parent::init();
        if (empty($this->cLoginUser)) {
            $this->redirect($this->createUrl('/site/login'));
        } else {
            $userLoginUser = $this->cLoginUser;
            $this->user_role_list = isset($userLoginUser['cuser']['role_id']) ? $userLoginUser['cuser']['role_id'] : '';
            $this->user_school_list = isset($userLoginUser['cuser']['school']['school_id']) ? $userLoginUser['cuser']['school']['school_id'] : '';
            $this->user_subject_list = isset($userLoginUser['cuser']['school']['subject_id']) ? $userLoginUser['cuser']['school']['subject_id'] : '';
            $zRow = new ZRole();
            //得到学校对应的考试
            $exam_list = $zRow->_getSchoolExam($this->user_school_list);
            if (!empty($exam_list)) {
                $this->exam_id = implode(',', $exam_list);
            }
        }
        $this->commonScript();
        Yii::app()->clientScript->registerCssFile(THEME_CSS_URL . '/module_Sip.css');
    }

    /*
     * 题号维护及分配
     */

    public function actionIndex() {
        $this->__common();
        $this->title = '题号维护及分配';
        $s = array();
        $s['exam_id'] = (int) $this->request->getParam('exam_id');
        $s['subject_id'] = (int) $this->request->getParam('subject_id');
        $s['subject_num'] = (int) $this->request->getParam('subject_num');
        $where = $wh = '';
        if (empty($s['exam_id'])) {
            //权限判断
            if ($this->user_role_list != 1 && !empty($this->exam_id)) {
                $where = ' WHERE "exam_id" IN (' . $this->exam_id . ')';
                $wh = ' AND "exam_id" IN (' . $this->exam_id . ')';
                if(!empty($this->user_subject_list)){
                    $where .= " AND \"subject_id\" IN (" . $this->user_subject_list . ")";
                }
            } else {
                $where = 'WHERE 1=1 ';
            }
        } else {
            $where = ' WHERE "exam_id" = ' . $s['exam_id'];
        }
        $exam = ZTask::_getExamAll($wh);
        $examdata = new ExamData();
        if ($this->user_role_list != 1) {//科目权限设定
		
            $subjectall = $examdata->_getSubjectList($this->user_subject_list);
        }else{
            $subjectall = $examdata->_getSubject();
        }
		
        $sql = ' SELECT "id","exam_id","subject_id","teacher_id","num_id","num_type" FROM "{{teacher_num}}" ' . $where;
        $sqlcount = ' SELECT count(*) FROM "{{teacher_num}}" ' . $where;
        if (isset($s['exam_id']) && $s['exam_id'] > 0) {
            $sql.= ' AND  "exam_id" = ' . $s["exam_id"];
            $sqlcount.=' AND  "exam_id" = ' . $s["exam_id"];
        }
        if (isset($s['subject_id']) && $s['subject_id'] > 0) {
            $sql.= ' AND  "subject_id" = ' . $s['subject_id'];
            $sqlcount.= ' AND  "subject_id" = ' . $s['subject_id'];
        }
        if ($s['subject_num'] > 0) {
            $sql.= ' AND "num_type" = ' . $s['subject_num'];
            $sqlcount.= ' AND "num_type" = ' . $s['subject_num'];
        }
        $sql.=' ORDER BY "id" DESC ';
        $data = ZCommon::getDaoPageList($sql, $sqlcount, 8);
        foreach ($data['listDatas'] as $key => $one) {
            $task_exam = ZTask::_getExamOne($one['exam_id']);
            if ($task_exam) {
                $data['listDatas'][$key]['exam_term_name'] = $task_exam['exam_term_name'];
            } else {
                $data['listDatas'][$key]['exam_term_name'] = '';
            }
            $subject = ExamData::_getSubject($one['subject_id']);
            if ($subject) {
                $data['listDatas'][$key]['subject_name'] = $subject['subject_name'];
            } else {
                $data['listDatas'][$key]['subject_name'] = '';
            }
            $subjecttype = ZTask::_getOnequestionType($one['num_type']);
            if ($subjecttype) {
                $data['listDatas'][$key]['question_name'] = $subjecttype['question_name'];
            } else {
                $data['listDatas'][$key]['question_name'] = '';
            }
            $datateacher = array();
            if ($one['teacher_id']) {
                $sql_teacher = 'SELECT "teacher_name" FROM "exm_teacher" WHERE "teacher_id" in (' . $one['teacher_id'] . ')';
                $datateacher = Yii::app()->db->createCommand($sql_teacher)->queryAll();
            }
            $data['listDatas'][$key]['teacher'] = $datateacher;
        }
        $pageUrl = $this->createUrl('/problem/question/index', $s);
        $this->render('index', array(
            'exam' => $exam,
            'subjectall' => $subjectall,
            'data' => $data['listDatas'],
            'pages' => $data['pages'],
            'search' => $s,
            'pageUrl' => $pageUrl
        ));
    }

    /*
     * 添加 题号
     */

    public function actionQuestionadd() {
        $this->__common();
        $where = $wh = '';
        if ($this->user_role_list != 1) {
            $wh = ' AND "exam_id" IN (' . $this->exam_id . ')';
        }
        $exam = ZTask::_getExamAll($wh);
        $examdata = new ExamData();
        if ($this->user_role_list != 1) {//科目权限设定
            $subjectall = $examdata->_getSubjectList($this->user_subject_list);
        }else{
        $subjectall = $examdata->_getSubject();
        }
        /** 添加入库* */
        if ($_POST) {
            $teacher_ids = null;
            if (isset($_POST['teacher_ids'])) {
                $teacher_ids = implode(',', $_POST['teacher_ids']);
            }
            $newteacher = new TeacherNum();
            $data = $newteacher->count('"exam_id" =' . $_POST['exam_id'] . ' AND "subject_id" = ' . $_POST['subject_id'] . ' AND "num_id" =\'' . $_POST['num_id'] . '\'');
            if ($data > 0) {
                $this->frameAjaxMessage(null, '该题号已存在请仔细确认!');
                exit;
            }
            $teachernum = new TeacherNum();
            $teachernum->exam_id = $_POST['exam_id'];
            $teachernum->subject_id = $_POST['subject_id'];
            $teachernum->num_id = $_POST['num_id'];
            $teachernum->num_type = $_POST['num_type'];
            $teachernum->teacher_id = $teacher_ids;

            $teachernum->create_user = $this->cLoginUser['cuser']['id'];
            $teachernum->create_time = time();
            $teachernum->update_user = $this->cLoginUser['cuser']['id'];
            $teachernum->update_time = time();
            if ($teachernum->save()) {
                if (isset($_POST['teacher_group_ids'])) {
                    foreach ($_POST['teacher_group_ids'] as $one) {
                        $teacher = new Teacher();
                        $teacherinfo = $teacher->findByPk($one);
                        $teacherinfo->leader = 1;
                        $teacherinfo->update();
                    }
                }
                $this->frameAjaxMessage('/problem/question/index');
            } else {
                exit;
            }
        }
        $this->renderPartial("questionadd", array('exam' => $exam, 'subjectall' => $subjectall), true);
    }

    public function actionQuestionteacher() {
        $exam_id = (int) $this->request->getParam('exam_id');
        $subject_id = (int) $this->request->getParam('subject_id');
        $teacher = ZProblemNum::_getAllproblemteacher($exam_id, $subject_id);
        $html = "";
        if ($teacher) {
            foreach ($teacher as $key => $one) {
                $html .= " <option value = " . $one['teacher_id'] . " >" . $one['teacher_name'] . "</option>";
            }
        }
        echo $html;
        exit;
    }

    /*
     * 编辑
     */

    public function actionQuestionedit() {
        $this->__common();
        $where = $wh = '';
        if ($this->user_role_list != 1) {
            $wh = ' AND "exam_id" IN (' . $this->exam_id . ')';
        }
        $exam = ZTask::_getExamAll($wh);
        $examdata = new ExamData();
        if ($this->user_role_list != 1) {//科目权限设定
            $subjectall = $examdata->_getSubjectList($this->user_subject_list);
        }else{
            $subjectall = $examdata->_getSubject();
        }
        $id = (int) $this->request->getParam('id');
        $teachernum = new TeacherNum();
        $teacher = array();
        $teachernuminfo = $teachernum->findByPk($id);
        $user_teacher = $teacher_iu = $teacher_groop = array();
        if ($teachernuminfo) {
            $teachernuminfo = $teachernuminfo->attributes;
            $teacher = ZProblemNum::_getAllproblemteacher($teachernuminfo['exam_id'], $teachernuminfo['subject_id']);
            if (!empty($teachernuminfo['teacher_id'])) {
                $where = ' AND "teacher_id" in (' . $teachernuminfo['teacher_id'] . ')';
                $user_teacher = ZProblemNum::_getAllproblemteacher($teachernuminfo['exam_id'], $teachernuminfo['subject_id'], $where);
                if ($user_teacher) {
                    foreach ($user_teacher as $key => $one) {
                        if (!empty($one['leader'])) {
                            $teacher_groop = $one;
                        }
                        $teacher_iu[] = $one['teacher_id'];
                    }
                }
            }
        }

        /** 添加入库* */
        if ($_POST) {
            $newteacher = new TeacherNum();
            $data = $newteacher->count('"id" != ' . $_POST['id'] . ' AND "exam_id" =' . $_POST['exam_id'] . ' AND "subject_id" = ' . $_POST['subject_id'] . ' AND "num_id" =\'' . $_POST['num_id'] . '\'');
            if ($data > 0) {
                $this->frameAjaxMessage(null, '该题号已存在请仔细确认!');
                exit;
            }
            $teachernum = new TeacherNum();
            $teachernuminfo = $teachernum->findByPk($_POST['id']);
            $teacher_ids = null;
            if (isset($_POST['teacher_ids'])) {
                $teacher_ids = implode(',', $_POST['teacher_ids']);
                if (count($_POST['teacher_ids']) > 0) {
                    foreach ($_POST['teacher_ids'] as $key => $two) {
                        $teacher = new Teacher();
                        $teacherinfo = $teacher->findByPk($two);
                        $teacherinfo->leader = null;
                        $teacherinfo->update();
                    }
                }
            }
            $teachernuminfo->exam_id = $_POST['exam_id'];
            $teachernuminfo->subject_id = $_POST['subject_id'];
            $teachernuminfo->num_id = $_POST['num_id'];
            $teachernuminfo->num_type = $_POST['num_type'];
            $teachernuminfo->teacher_id = $teacher_ids;
            $teachernum->update_user = $this->cLoginUser['cuser']['id'];
            $teachernum->update_time = time();
            if ($teachernuminfo->update()) {
                if (isset($_POST['teacher_group_ids'])) {
                    foreach ($_POST['teacher_group_ids'] as $one) {
                        $teacher = new Teacher();
                        $teacherinfo = $teacher->findByPk($one);
                        $teacherinfo->leader = 1;
                        $teacherinfo->update();
                    }
                }
                $this->frameAjaxMessage('/problem/question/index',true);
                exit;
            } else {
                exit;
            }
        }
        $this->renderPartial("questionedit", array('teachernuminfo' => $teachernuminfo, 'exam' => $exam, 'subjectall' => $subjectall, 'teacher' => $teacher, 'user_teacher' => $user_teacher, 'teacher_iu' => $teacher_iu, 'teacher_groop' => $teacher_groop), true);
    }

    /*
     * 删除
     */

    public function actionDelquestione() {
        $this->__common();
        $id = (string) $this->request->getParam('id');
        $teachernum = new TeacherNum();
        if ($teachernum->deleteByPk($id)) {
            echo 1;
        } else {
            echo 2;
        }
    }

    /*
     * 批量添加考题
     */
    /*
     * 添加 题号
     */

    public function actionAlladd() {
        $this->__common();
        $exam = ZTask::_getExamAll();
        $examdata = new ExamData();
        $subjectall = $examdata->_getSubject();
        if ($_POST) {
            $sum = array();
            $start_str = $end_str = '';
            $a = substr_count($_POST['num_id'], '(*)');
            if ($a > 0) {
                $strlen_b = strlen($_POST['num_id']);
                $strlen_a = strlen('(*)');
                $aa = strpos($_POST['num_id'], '(*)');
                $start_str = substr($_POST['num_id'], 0, $aa);
                $end_str = substr($_POST['num_id'], $aa + $strlen_a, $strlen_b);
            }
            if ($_POST['gz'] == 1) {
                for ($i = $_POST['start']; $i <= $_POST['end']; $i++) {
                    $nn = '';
                    for ($j = 1; $j <= $_POST['length'] - strlen($i); $j++) {
                        $nn = "0" . $nn;
                    }
                    $sum[] = $start_str . $nn . $i . $end_str;
                }
            } else {
                $str = "abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ";
                for ($i = strpos($str, $_POST['astart']); $i <= strrpos($str, $_POST['aend']); $i++) {
                    $sum[] = $start_str . $str[$i] . $end_str;
                }
            }
            foreach ($sum as $one) {
                $newteacher = new TeacherNum();
                $data = $newteacher->count('"exam_id" =' . $_POST['exam_id'] . ' AND "subject_id" = ' . $_POST['subject_id'] . ' AND "num_id" = \'' . $one . '\'');
                if ($data > 0) {
                    $this->frameAjaxMessage(null, '存在已经生成的题号请仔细确认!');
                    exit;
                }
            }
            foreach ($sum as $one) {
                $teachernum = new TeacherNum();
                $teachernum->exam_id = $_POST['exam_id'];
                $teachernum->subject_id = $_POST['subject_id'];
                $teachernum->num_id = $one;
                $teachernum->num_type = $_POST['num_type'];
                $teachernum->save();
            }
            $this->redirect('/problem/question/index');
        }
        $this->renderPartial("questionall", array('exam' => $exam, 'subjectall' => $subjectall), true);
    }

    /**
     * @ 批量删除学段
     * @ yindebin
     * @ 2014-01-06 pm
     *
     */
    public function actionDelete_all() {
        $id = Yii::app()->request->getparam('id');
        if (!empty($id)) {
            $delete = 'DELETE FROM "{{teacher_num}}" WHERE "id" IN (' . $id . ')';
            $query = Yii::app()->db->createCommand($delete)->execute();
            echo 1;
        } else {
            echo 2;
        }
    }

}

?>
